// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database()
const _ = db.command

// 云函数入口函数
exports.main = async (event, context) => {
  // result = await db.collection('tickets').where({
  //   userid: event.userid
  // }).get()
  result = await db.collection('tickets').aggregate()
    .lookup({
      from: 'concert',
      localField: 'concertid',
      foreignField: '_id',
      as: 'orders',
    }).match({
      userid: event.userid
    })
    .end()
    // .then(res => console.log(res))
    // .catch(err => console.error(err))
    orderlist = result.list
    console.log(orderlist)
    console.log(orderlist.length)
    const orders = {}
    for(var i = 0; i < orderlist.length; i++){
      const order = {}
      order.concertid = orderlist[i].concertid
      order.src = orderlist[i].orders[0].cover_url
      order.title = orderlist[i].title
      order.time = orderlist[i].orders[0].date
      order.place = orderlist[i].orders[0].place
      console.log(order)
      orders[i] = order
    }
    return orders
}